<?php
class Default_IndexController extends Zend_Controller_Action {
	public function init() {
		/* Initialize action controller here */
		Zend_Layout::startMvc ( array (
				"layoutPath" => APPLICATION_PATH . "/layouts/scripts",
				"layout" => "layout" 
		) );
	}
	public function indexAction() {
		// action body
		$this->view->headTitle ( "Trang Chủ", "PREPEND" );
		$ca = new Default_Model_DbTable_Menu ();
		$this->view->allCat = $ca->getMenuByParent ( 3, 1 );
	}
	public function signinAction() {
		$this->_helper->layout ()->disableLayout ();
	}
	public function loginAction() {
		if ($this->_request->isPost ()) {
			// 1.Gá»�i káº¿t ná»‘i vá»›i Zend_Db
			$db = Zend_Registry::get ( "connectDB" );
			// $db = Zend_Db::factory($dbOption['adapter'],$dbOption['params']);
			// 2. Khoi tao Zend Auth
			$auth = Zend_Auth::getInstance ();
			
			// 3.Khai bÃ¡o báº£ng vÃ  2 cá»™t dÃ¹ng Ä‘á»ƒ so
			// sÃ¡nh khi login
			$authAdapter = new Zend_Auth_Adapter_DbTable ( $db );
			$authAdapter->setTableName ( "user" )->setIdentityColumn ( "EMAIL" )->setCredentialColumn ( "PASSWORD" );
			
			// 4.Láº¥y giÃ¡ trá»‹ Ä‘Æ°á»£c gá»­i tá»«
			// form
			$uname = $this->_request->getParam ( "email" );
			$pass = $this->_request->getParam ( "password" );
			
			// 5.Ä�Æ°a vÃ o so sÃ¡nh vá»›i dá»¯ liá»‡u
			$authAdapter->setIdentity ( $uname );
			$authAdapter->setCredential ( md5 ( "CTV" . md5 ( $pass ) . "ITTeam" ) );
			
			// 6.Kiá»ƒm tra tráº¡ng thÃ¡i cá»§a user náº¿u Status
			// = 1 thÃ¬ má»›i Ä‘Æ°á»£c login
			$select = $authAdapter->getDbSelect ();
			$select->where ( "STATUS = 1" );
			
			// 7.Láº¥y káº¿t quáº£ truy váº¥n
			$result = $auth->authenticate ( $authAdapter );
			$flag = false;
			if ($result->isValid ()) {
				// 8.Láº¥y nhá»¯ng dá»¯ liá»‡u cáº§n thiáº¿t
				// trong báº£ng admin náº¿u login thÃ nh cÃ´ng
				$data = $authAdapter->getResultRowObject ( null, array (
						"PASSWORD" 
				) );
				// 9.LÆ°u dá»¯ liá»‡u cá»§a admin vÃ o session
				$auth->getStorage ()->write ( $data );
				$flag = true;
			}
			if ($flag == true) {
				$this->_redirect ( "default/index" );
			} else {
				$this->_redirect ( "default/index" );
			}
		}
	}
	public function logoutAction() {
		$auth = Zend_Auth::getInstance ();
		$auth->clearIdentity ();
		$this->_redirect ( "/index" );
	}
}

